﻿using Prism.Services.Dialogs;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using XH.SmartParking.IService;
using XH.SmartParking.Entities;

namespace XH.SmartParking.ViewModels.Pages.Dialogs
{
    public class ModifyPasswordViewModel : DialogViewModelBase
    {
        IUserService _userService;
        public ModifyPasswordViewModel(IUserService userService)
        {
            _userService = userService;

            Title = "修改密码";
        }
        public override void OnDialogOpened(IDialogParameters parameters)
        {
            _pwd = parameters.GetValue<string>("pwd");
            _uid = parameters.GetValue<int>("uid");

            OldPassword = "";
            NewPassword = "";
            ConfirmPassword = "";
        }

        private int _uid;
        private string _pwd;

        private string _oldPassword;

        public string OldPassword
        {
            get { return _oldPassword; }
            set
            {
                _oldPassword = value;

                ErrorList.Remove("OldPassword");
                if (string.IsNullOrEmpty(value))
                {
                    ErrorList.Add("OldPassword", new List<string> { "旧密码不能为空" });
                    RaiseErrorsChanged();
                }
                else if (value != _pwd)
                {
                    ErrorList.Add("OldPassword", new List<string> { "旧密码不正确" });
                    RaiseErrorsChanged();
                }
            }
        }

        private string _newPassword;

        public string NewPassword
        {
            get { return _newPassword; }
            set
            {
                _newPassword = value;
                ErrorList.Remove("NewPassword");
                ErrorList.Remove("ConfirmPassword");
                if (string.IsNullOrEmpty(_oldPassword))
                {
                    ErrorList.Add("NewPassword", new List<string> { "新密码不能为空" });
                    RaiseErrorsChanged();
                }
                else if (ConfirmPassword != NewPassword)
                {
                    ErrorList.Add("ConfirmPassword", new List<string> { "两次新密码不一致" });
                    RaiseErrorsChanged("ConfirmPassword");
                }
            }
        }

        private string _confirmPassword;

        public string ConfirmPassword
        {
            get { return _confirmPassword; }
            set
            {
                _confirmPassword = value;
                ErrorList.Remove("ConfirmPassword");
                if (string.IsNullOrEmpty(_oldPassword))
                {
                    ErrorList.Add("ConfirmPassword", new List<string> { "确认密码不能为空" });
                    RaiseErrorsChanged();
                }
                else if (ConfirmPassword != NewPassword)
                {
                    ErrorList.Add("ConfirmPassword", new List<string> { "两次新密码不一致" });
                    RaiseErrorsChanged();
                }
            }
        }


        public override void DoSave()
        {
            try
            {
                // 检查旧密码是否正确
                // 检查新密码两次输入是否一致
                if (HasErrors) return;

                var user = _userService.Find<SysUser>(_uid);
                if (user == null) return;

                user.Password = NewPassword;
                _userService.Update(user);

                base.DoSave();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
